package day_06;

import java.util.Arrays;

/**
 * @ClassName BinaryInsert1
 * @Description TODO
 * @Author hp
 * @Date 2021/6/9 16:35
 * @Version 1.0
 **/
public class BinaryInsert1 {

    public static void main(String[] args) {

        int[] arr = {1, 4, 2, 7, 9, 8, 3, 6};

        for (int i = 1; i < arr.length; i++) {
            int temp = arr[i];
            if (temp > arr[i - 1]) {
                continue;
            }
            int left = 0;
            int right = i - 1;
            while (left <= right) {
                int mid = (left + right) / 2;
                if (temp < arr[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }
            System.arraycopy(arr,left,arr,left+1,i-left);
            arr[left]=temp;
        }

        System.out.println(Arrays.toString(arr));
    }

}
